// exports.LanguagePackage = void 0;
// var $pPSDK_Config = require("./AL_Framework_AL_SDK_CONFIG");
// var $resource = require("./AL_Framewrok_Resource");
// var a = (function () {
//     function e(e, t, o) {
//         this.root = "";
//         this.language = null;
//         this.label_array_list = [];
//         this.label_data = null;
//         this.sprite_array_list = [];
//         this.key = e;
//         this.bundle = t;
//         this.root = o || "";
//     }
//     e.prototype.shezhiYuyan = function (e) {
//         this.language = e;
//         this.chongxinJiazaiBiaoqian();
//         this.chongxinJiazaiJingling();
//     };
//     e.prototype.tianjiaHuoShanChuBiaoqian = function (e, t) {
//         if (t) {
//             this.label_array_list.push(e);
//         } else {
//             {
//                 var o = this.label_array_list.indexOf(e);
//                 if (-1 !== o) {
//                     this.label_array_list.splice(o, 1);
//                 }
//             }
//         }
//     };
//     e.prototype.huoqu_biaoqian_neirong = function (e, t) {
//         if (!this.label_data) {
//             return e;
//         }
//         if (!t) {
//             return this.label_data[e] || e;
//         }
//         if (0 === t.length) {
//             return this.label_data[e] || e;
//         }
//         var o = this.label_data[e] || e;
//         for (var i = 0; i < t.length; i++) {
//             var n = new RegExp("#" + i, "g");
//             o = o.replace(n, t[i]);
//         }
//         return o;
//     };
//     e.prototype.tianjiaHuoShanchuJingling = function (e, t) {
//         if (t) {
//             this.sprite_array_list.push(e);
//         } else {
//             {
//                 var o = this.sprite_array_list.indexOf(e);
//                 if (-1 !== o) {
//                     this.sprite_array_list.splice(o, 1);
//                 }
//             }
//         }
//     };
//     e.prototype.huoquJingling = function (e, t) {
//         var o = this;
//         var i = this.huoquJinglingPeizhiUrl(e);
//         var a = this.bundle;
//         Manager.cacheManager.yibuHuoquHuancun(i, cc.SpriteFrame, a).then(function (e) {
//             if (e) {
//                 var s = "framework" == o.bundle;
//                 var r = new $resource.AL_Framewrok_Resource.Info();
//                 r.url = i;
//                 r.type = cc.SpriteFrame;
//                 r.data = e;
//                 r.retain = s;
//                 r.bundle = a;
//                 t(e);
//             } else {
//                 t(null);
//             }
//         });
//     };
//     e.prototype.chongxinJiazaiBiaoqian = function () {
//         var e = this;
//         return new Promise(function (t, o) {
//             Manager.cacheManager.yibuHuoquHuancun(e.huoquBiaoqianPeizhiUrl(), cc.JsonAsset, e.bundle).then(function (i) {
//                 if (i) {
//                     e.label_data = i.json;
//                     var n = 0;
//                     for (var a = e.label_array_list; n < a.length; n++) {
//                         a[n].chongzhiZhi();
//                     }
//                     t();
//                 } else {
//                     o();
//                 }
//             });
//         });
//     };
//     e.prototype.chongxinJiazaiJingling = function () {
//         var e = 0;
//         for (var t = this.sprite_array_list; e < t.length; e++) {
//             t[e].chongzhiZhi();
//         }
//     };
//     e.prototype.chongxinJiazaiMuluxiaJingling = function () {
//         var e = this;
//         return new Promise(function (t) {
//             Manager.assetManager.loadDir({
//                 bundle: e.bundle,
//                 path: e.huoquJinglingPeizhiUrl(""),
//                 type: cc.SpriteFrame,
//                 onComplete: function (e) {
//                     console.log("$$$$$$$$$$$$$:", e);
//                     t();
//                 }
//             });
//         });
//     };
//     e.prototype.huoquBiaoqianPeizhiUrl = function () {
//         return this.root + "/" + $pPSDK_Config.LanguageConfig.GENERAL_LANGUAGE_LABEL_ROOT + "/" + this.language;
//     };
//     e.prototype.huoquJinglingPeizhiUrl = function (e) {
//         return (
//             this.root + "/" + $pPSDK_Config.LanguageConfig.GENERAL_LANGUAGE_SPRITE_ROOT + "/" + this.language + "/" + e
//         );
//     };
//     return e;
// })();
// exports.LanguagePackage = a;
// var s = (function () {
//     function e() {
//         this.logTag = "[AL_Framework_Language_Mgr]";
//         this.map_package = {};
//         this.language = null;
//     }
//     e.Instance = function () {
//         return this._instance || (this._instance = new e());
//     };
//     e.prototype.jianchaBingChushihua = function () {
//         if (!this.language) {
//             this.shezhiYuyan("zh", !0);
//         }
//     };
//     e.prototype.shezhiYuyan = function (e, t) {
//         if (void 0 === t) {
//             t = !1;
//         }
//         if ($pPSDK_Config.LanguageConfig.ENABLE_CHANGE_LANGUAGE || t) {
//             if (this.language != e) {
//                 for (var o in ((this.language = e), this.map_package)) this.map_package[o].shezhiYuyan(e);
//             }
//         } else {
//             Log.w(this.logTag, "配置不允许启动后切换语言");
//         }
//     };
//     e.prototype.getLanguage = function () {
//         return this.language;
//     };
//     e.prototype.zhuceYuyanbao = function (e, t, o) {
//         if (e in this.map_package == 0) {
//             var i = new a(e, t, o);
//             this.map_package[e] = i;
//         }
//         if (this.language) {
//             this.map_package[e].shezhiYuyan(this.language);
//         }
//     };
//     e.prototype.yichuYuyanbao = function (e) {
//         delete this.map_package[e];
//     };
//     e.prototype.tianjiaHuoShanChuBiaoqian = function (e, t, o) {
//         if (this.jianchaYuyanbaoShifouCunzai(e)) {
//             this.map_package[e].tianjiaHuoShanChuBiaoqian(t, o);
//         }
//     };
//     e.prototype.huoqu_biaoqian_neirong = function (e, t, o) {
//         return this.jianchaYuyanbaoShifouCunzai(e) ? (this.jianchaBingChushihua(), this.map_package[e].huoqu_biaoqian_neirong(t, o)) : t;
//     };
//     e.prototype.tianjiaHuoShanchuJingling = function (e, t, o) {
//         if (this.jianchaYuyanbaoShifouCunzai(e)) {
//             this.map_package[e].tianjiaHuoShanchuJingling(t, o);
//         }
//     };
//     e.prototype.huoquJingling = function (e, t, o) {
//         if (this.jianchaYuyanbaoShifouCunzai(e)) {
//             this.jianchaBingChushihua();
//             this.map_package[e].huoquJingling(t, o);
//         }
//     };
//     e.prototype.chongxinJiazaiBiaoqian = function (e) {
//         if (this.jianchaYuyanbaoShifouCunzai(e)) {
//             this.jianchaBingChushihua();
//             return this.map_package[e].chongxinJiazaiBiaoqian();
//         }
//     };
//     e.prototype.chongxinJiazaiJingling = function (e) {
//         if (this.jianchaYuyanbaoShifouCunzai(e)) {
//             this.jianchaBingChushihua();
//             return this.map_package[e].chongxinJiazaiMuluxiaJingling();
//         }
//     };
//     e.prototype.jianchaYuyanbaoShifouCunzai = function (e) {
//         return (
//             e in this.map_package == 1 ||
//             (Log.d(this.logTag, "语言包未注册,新注册语言包" + e), this.zhuceYuyanbao(e, e), !0)
//         );
//     };
//     e._instance = null;
//     return e;
// })();
// exports.default = s;


const $pPSDK_Config = require("./AL_Framework_AL_SDK_CONFIG");
const $resource = require("./AL_Framewrok_Resource");

/**
 * 语言包类
 */
class LanguagePackage {
    constructor(key, bundle, root = "") {
        /** 根路径 */
        this.root = root;
        /** 当前语言 */
        this.language = null;
        /** 标签数组 */
        this.label_array_list = [];
        /** 标签数据 */
        this.label_data = null;
        /** 精灵数组 */
        this.sprite_array_list = [];
        /** 关键字 */
        this.key = key;
        /** 绑定 */
        this.bundle = bundle;
    }

    /**
     * 设置语言并重新加载标签和精灵
     * @param {string} language 语言
     */
    shezhiYuyan(language) {
        this.language = language;
        this.chongxinJiazaiBiaoqian();
        this.chongxinJiazaiJingling();
    }

    /**
     * 添加或删除标签
     * @param {Object} label 标签
     * @param {boolean} isAdd 是否添加
     */
    tianjiaHuoShanChuBiaoqian(label, isAdd) {
        if (isAdd) {
            this.label_array_list.push(label);
        } else {
            const index = this.label_array_list.indexOf(label);
            if (index !== -1) {
                this.label_array_list.splice(index, 1);
            }
        }
    }

    /**
     * 获取标签内容
     * @param {string} key 标签键
     * @param {Array<string>} params 参数
     * @returns {string} 标签内容
     */
    huoqu_biaoqian_neirong(key, params) {
        if (!this.label_data) {
            return key;
        }
        if (!params || params.length === 0) {
            return this.label_data[key] || key;
        }
        let label = this.label_data[key] || key;
        params.forEach((param, index) => {
            label = label.replace(new RegExp(`#${index}`, "g"), param);
        });
        return label;
    }

    /**
     * 添加或删除精灵
     * @param {Object} sprite 精灵
     * @param {boolean} isAdd 是否添加
     */
    tianjiaHuoShanchuJingling(sprite, isAdd) {
        if (isAdd) {
            this.sprite_array_list.push(sprite);
        } else {
            const index = this.sprite_array_list.indexOf(sprite);
            if (index !== -1) {
                this.sprite_array_list.splice(index, 1);
            }
        }
    }

    /**
     * 获取精灵
     * @param {string} key 精灵键
     * @param {Function} callback 回调函数
     */
    huoquJingling(key, callback) {
        const spriteUrl = this.huoquJinglingPeizhiUrl(key);
        const bundle = this.bundle;

        Manager.cacheManager.yibuHuoquHuancun(spriteUrl, cc.SpriteFrame, bundle).then((spriteFrame) => {
            if (spriteFrame) {
                const isFrameworkBundle = bundle === "framework";
                const resourceInfo = new $resource.AL_Framewrok_Resource.Info();
                resourceInfo.url = spriteUrl;
                resourceInfo.type = cc.SpriteFrame;
                resourceInfo.data = spriteFrame;
                resourceInfo.retain = isFrameworkBundle;
                resourceInfo.bundle = bundle;
                callback(spriteFrame);
            } else {
                callback(null);
            }
        });
    }

    /**
     * 重新加载标签
     * @returns {Promise}
     */
    chongxinJiazaiBiaoqian() {
        return new Promise((resolve, reject) => {
            Manager.cacheManager.yibuHuoquHuancun(this.huoquBiaoqianPeizhiUrl(), cc.JsonAsset, this.bundle).then((jsonAsset) => {
                if (jsonAsset) {
                    this.label_data = jsonAsset.json;
                    this.label_array_list.forEach(label => label.chongzhiZhi());
                    resolve();
                } else {
                    reject();
                }
            });
        });
    }

    /**
     * 重新加载精灵
     */
    chongxinJiazaiJingling() {
        this.sprite_array_list.forEach(sprite => sprite.chongzhiZhi());
    }

    /**
     * 重新加载目录下的精灵
     * @returns {Promise}
     */
    chongxinJiazaiMuluxiaJingling() {
        return new Promise((resolve) => {
            Manager.assetManager.loadDir({
                bundle: this.bundle,
                path: this.huoquJinglingPeizhiUrl(""),
                type: cc.SpriteFrame,
                onComplete: (sprites) => {
                    console.log("$$$$$$$$$$$$$:", sprites);
                    resolve();
                }
            });
        });
    }

    /**
     * 获取标签配置URL
     * @returns {string} 标签配置URL
     */
    huoquBiaoqianPeizhiUrl() {
        return `${this.root}/${$pPSDK_Config.LanguageConfig.GENERAL_LANGUAGE_LABEL_ROOT}/${this.language}`;
    }

    /**
     * 获取精灵URL
     * @param {string} key 精灵键
     * @returns {string} 精灵URL
     */
    huoquJinglingPeizhiUrl(key) {
        return `${this.root}/${$pPSDK_Config.LanguageConfig.GENERAL_LANGUAGE_SPRITE_ROOT}/${this.language}/${key}`;
    }
}

exports.LanguagePackage = LanguagePackage;

/**
 * 语言管理类
 */
class AL_Framework_Language_Mgr {
    constructor() {
        /** 日志标签 */
        this.logTag = "[AL_Framework_Language_Mgr]";
        /** 语言包映射 */
        this.map_package = {};
        /** 当前语言 */
        this.language = null;
    }

    /**
     * 获取单例实例
     * @returns {AL_Framework_Language_Mgr} 单例实例
     */
    static Instance() {
        return this._instance || (this._instance = new AL_Framework_Language_Mgr());
    }

    /**
     * 检查并初始化
     */
    jianchaBingChushihua() {
        if (!this.language) {
            this.shezhiYuyan("zh", true);
        }
    }

    /**
     * 设置语言
     * @param {string} newLanguage 新语言
     * @param {boolean} force 是否强制设置
     */
    shezhiYuyan(newLanguage, force = false) {
        if ($pPSDK_Config.LanguageConfig.ENABLE_CHANGE_LANGUAGE || force) {
            if (this.language !== newLanguage) {
                this.language = newLanguage;
                for (const key in this.map_package) {
                    this.map_package[key].shezhiYuyan(newLanguage);
                }
            }
        } else {
            Log.w(this.logTag, "配置不允许启动后切换语言");
        }
    }

    /**
     * 获取当前语言
     * @returns {string} 当前语言
     */
    getLanguage() {
        return this.language;
    }

    /**
     * 注册语言包
     * @param {string} key 语言包键
     * @param {string} bundle 绑定
     * @param {string} root 根路径
     */
    zhuceYuyanbao(key, bundle, root) {
        if (!(key in this.map_package)) {
            this.map_package[key] = new LanguagePackage(key, bundle, root);
        }
        if (this.language) {
            this.map_package[key].shezhiYuyan(this.language);
        }
    }

    /**
     * 移除语言包
     * @param {string} key 语言包键
     */
    yichuYuyanbao(key) {
        delete this.map_package[key];
    }

    /**
     * 添加或删除标签
     * @param {string} key 语言包键
     * @param {Object} label 标签
     * @param {boolean} isAdd 是否添加
     */
    tianjiaHuoShanChuBiaoqian(key, label, isAdd) {
        if (this.jianchaYuyanbaoShifouCunzai(key)) {
            this.map_package[key].tianjiaHuoShanChuBiaoqian(label, isAdd);
        }
    }

    /**
     * 获取标签
     * @param {string} key 语言包键
     * @param {string} label 标签键
     * @param {Array<string>} params 参数
     * @returns {string} 标签内容
     */
    huoqu_biaoqian_neirong(key, label, params) {
        if (this.jianchaYuyanbaoShifouCunzai(key)) {
            this.jianchaBingChushihua();
            return this.map_package[key].huoqu_biaoqian_neirong(label, params);
        }
        return label;
    }

    /**
     * 添加或删除精灵
     * @param {string} key 语言包键
     * @param {Object} sprite 精灵
     * @param {boolean} isAdd 是否添加
     */
    tianjiaHuoShanchuJingling(key, sprite, isAdd) {
        if (this.jianchaYuyanbaoShifouCunzai(key)) {
            this.map_package[key].tianjiaHuoShanchuJingling(sprite, isAdd);
        }
    }

    /**
     * 获取精灵
     * @param {string} key 语言包键
     * @param {string} spriteKey 精灵键
     * @param {Function} callback 回调函数
     */
    huoquJingling(key, spriteKey, callback) {
        if (this.jianchaYuyanbaoShifouCunzai(key)) {
            this.jianchaBingChushihua();
            this.map_package[key].huoquJingling(spriteKey, callback);
        }
    }

    /**
     * 重新加载标签
     * @param {string} key 语言包键
     * @returns {Promise}
     */
    chongxinJiazaiBiaoqian(key) {
        if (this.jianchaYuyanbaoShifouCunzai(key)) {
            this.jianchaBingChushihua();
            return this.map_package[key].chongxinJiazaiBiaoqian();
        }
    }

    /**
     * 重新加载精灵
     * @param {string} key 语言包键
     * @returns {Promise}
     */
    chongxinJiazaiJingling(key) {
        if (this.jianchaYuyanbaoShifouCunzai(key)) {
            this.jianchaBingChushihua();
            return this.map_package[key].chongxinJiazaiMuluxiaJingling();
        }
    }

    /**
     * 检查语言包是否存在
     * @param {string} key 语言包键
     * @returns {boolean} 是否存在
     */
    jianchaYuyanbaoShifouCunzai(key) {
        if (key in this.map_package) {
            return true;
        }
        Log.d(this.logTag, `语言包未注册,新注册语言包${key}`);
        this.zhuceYuyanbao(key, key);
        return true;
    }
}

AL_Framework_Language_Mgr._instance = null;

exports.default = AL_Framework_Language_Mgr;
